<template>
	<ge-page id="ge-page" :vanNavBarObj="vanNavBarObj">
		<view class="img-code"  @click="scan">
			<van-icon name="scan" size="120rpx" color="#fff"></van-icon>
		</view>
		<view class="sao-button" @click="scan">
			扫码核销
		</view>
	</ge-page>
</template>

<script>
	import wx from 'weixin-js-sdk';
	import { storageKey,appConfig } from '@/constant/config';
	export default {
		data() {
			return {
				vanNavBarObj: {
					title: '扫码核销',
				},
				orderNo:'',
                userId:0,
				token:'',
				userInfo:{}
			}
		},
		created() { 
			this.getConfig()
			this.userId = uni.getStorageSync(storageKey.userId)
			this.userInfo = uni.getStorageSync(storageKey.userInfo)
			this.token = uni.getStorageSync(storageKey.token)
		},
		onLoad() {
             
		},
		methods: {
			getConfig(){
				let baseUrl = encodeURIComponent(`${appConfig.domain}/storeadmin/`)
				uni.request({
					url: `${appConfig.domain}/api/index/getWxConfig?url=`+baseUrl,
					dataType: 'JSON',
					success: (res) => {
						const { appId, nonceStr, timestamp, signature } = res.data.data;
						console.log("获取配置",res.data.data)
						wx.config({
							debug: false, //开启调试模式
							appId: appId,
							timestamp: timestamp,
							nonceStr: nonceStr,
							signature: signature,
							jsApiList: ["scanQRCode"],
						})
					}
				})
			},
			scan() { 
				let app = this
				if(!app.userId){
					alert("请先登录再操作")
					return
				}
				wx.ready(() => {
					wx.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						scanType: ["qrCode"], // 可以指定扫二维码还是一维码，默认二者都有
						success: (res) => {
							var result = res.resultStr; // 扫描结果  
							app.orderNo = result
							app.writeOff()
							 
						}
					});
				})
				wx.error(function(res){
					console.log("错误提示",res)
				})
			},
			writeOff(orderNo){
				let app = this 
				uni.request({
					url: `${appConfig.domain}/store/order.delivery/deliverybyscan`,
					dataType: 'JSON',
					method: "POST",
					header: {  
					        'content-type': 'application/json', // 如果你需要的话，设置内容类型  
					        'Access-Token': app.token // 设置你的Access-Token  
					}, 
					data: {
						orderNo: app.orderNo
					},
					success(res) {
						//alert("res :"+JSON.stringify(res));
						let data = res.data
						if(data.status == 200){
							uni.showToast({  
							            title: '核销成功',  
							            icon: 'success',  
							            duration: 2000 // 提示框的显示时间，单位毫秒，默认1500  
							}); 
							 setTimeout(() => {  
							             // 跳转到目标页面  
							             //页面跳转到订单列表
							           window.location.href = '/storeadmin#/pages/order/detail/index?orderid='+data.data['order_id'];
							}, 2000);
							
						}else{
							alert("核销失败:"+data.message)
						}
					},
					fail(e) {
						alert(e);
					},
					complete(res) {
						 
					},
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #fafafa;
	}

	.img-code {
		background-color: blue;
		height: 350rpx;
		width: 350rpx;
		border-radius: 50%;
		margin: 100rpx auto 200rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		.sao-icon {
			width: 200rpx;
			height: 200rpx;
		}
	}

	.sao-button {
		width: 200rpx;
		height: 80rpx;
		background-color: blue;
		color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 40rpx;
		margin: 0 auto;
	}
</style>